require "common/Define"
require "framework/BaseController"
require "FairyGUI"

TransitionMain = BaseController:New({mControllerName = "TransitionMain"});

function TransitionMain:Main()
	-- body
	TransitionMain:Awake();
end

function TransitionMain:Awake()
	Application.targetFrameRate = 60;
	Stage.inst.onKeyDown:Add(self.OnKeyDown);

	-- load ui
	local prePackageName = "UI/";
	local packageName = "Transition";
	UIPackage.AddPackage(prePackageName..packageName);

	local componentName = "Main";
	self.mainComponent = UIPackage.CreateObject(packageName, componentName).asCom;
	GRoot.inst:AddChild( self.mainComponent );

	print(self)

	self._startValue = 0;
	self._endValue = 0;

	self:OnViewCreated(self.mainComponent);
end

function TransitionMain:OnKeyDown(context)
 	-- body
 	if (context.inputEvent.keyCode == KeyCode.Escape) then
		print "Exit Game!"
		Application.Quit();
	end
end

function TransitionMain:InitView()
	self._btnGroup = self.mainComponent:GetChild("g0").asGroup;

	self._g1 = UIPackage.CreateObject("Transition", "BOSS").asCom;
	self._g2 = UIPackage.CreateObject("Transition", "BOSS_SKILL").asCom;
	self._g3 = UIPackage.CreateObject("Transition", "TRAP").asCom;
	self._g4 = UIPackage.CreateObject("Transition", "GoodHit").asCom;
	self._g5 = UIPackage.CreateObject("Transition", "PowerUp").asCom;
	self._g5:GetTransition("t0"):SetHook("play_num_now", TransitionMain.__playNum);

	self.mainComponent:GetChild("btn0").onClick:Add( function () TransitionMain.__play(TransitionMain._g1) end );
	self.mainComponent:GetChild("btn1").onClick:Add( function () TransitionMain.__play(TransitionMain._g2) end );
	self.mainComponent:GetChild("btn2").onClick:Add( function () TransitionMain.__play(TransitionMain._g3) end );
	self.mainComponent:GetChild("btn3").onClick:Add( TransitionMain.__play4 );
	self.mainComponent:GetChild("btn4").onClick:Add( TransitionMain.__play5 );
end

function TransitionMain.__play4()
	-- body
	TransitionMain._btnGroup.visible = false;
	TransitionMain._g4.x = GRoot.inst.width - TransitionMain._g4.width - 20;
	TransitionMain._g4.y = 100;
	GRoot.inst:AddChild(TransitionMain._g4);

	local t = TransitionMain._g4:GetTransition("t0");
	t:Play(3, 0, function ()
			TransitionMain._btnGroup.visible = true;
			GRoot.inst:RemoveChild(TransitionMain._g4);
			end
		);
end

function TransitionMain.__play5()
	-- body
	TransitionMain._btnGroup.visible = false;
	TransitionMain._g5.x = 20;
	TransitionMain._g5.y = GRoot.inst.height - TransitionMain._g5.height - 100;
	GRoot.inst:AddChild(TransitionMain._g5);

	local t = TransitionMain._g5:GetTransition("t0");
	TransitionMain._startValue = 10000;
	
	math.randomseed(tostring(os.time()):reverse():sub(1, 6));
	local add = math.random(1000, 3000);
	add = Mathf.Floor(add);
	print("add "..add);
	TransitionMain._endValue = TransitionMain._startValue + add;
	TransitionMain._g5:GetChild("value").text = ""..TransitionMain._startValue;
	TransitionMain._g5:GetChild("add_value").text = ""..add;
	t:Play(function ()
			TransitionMain._btnGroup.visible = true;
			GRoot.inst:RemoveChild(_g5);
		end
		);
end

function TransitionMain.__playNum()
	print("__playNum");

	DG.Tweening.DOTween.To( DG.Tweening.Core.DOGetter_float(TransitionMain.Extension_funGetter), DG.Tweening.Core.DOSetter_float(TransitionMain.Extension_funSetter), TransitionMain._endValue, 0.3):SetEase(DG.Tweening.Ease.Linear):SetUpdate(true);
end

function TransitionMain.Extension_funGetter()
	print("getter", TransitionMain._startValue); 
	return TransitionMain._startValue;
end

function TransitionMain.Extension_funSetter(x)
	print("setter", TransitionMain._g5); 
	TransitionMain._g5:GetChild("value").text = ""..Mathf.Floor(x); 
end

function TransitionMain.__play(target)
	-- body
	TransitionMain._btnGroup.visible = false;
	GRoot.inst:AddChild(target);
	local t = target:GetTransition("t0");
	t:Play( function ()
			TransitionMain._btnGroup.visible = true;
			GRoot.inst:RemoveChild(target);
			end
		)
end